#pragma once
#include<assert.h>
#include"reverse_iterator.h"
namespace ljw
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;
		typedef ReverseIterator<iterator, T&, T*> reverse_iterator;
		typedef ReverseIterator<const_iterator, const T&, const T*> const_reverse_iterator;
		reverse_iterator rbegin()
		{
			return reverse_iterator(end()-1);
		}
		reverse_iterator rend()
		{
			return reverse_iterator(begin()-1);
		}
		const_reverse_iterator rbegin()const
		{
			return const_reverse_iterator(end() - 1);
		}
		const_reverse_iterator rend()const
		{
			return const_reverse_iterator(begin() - 1);
		}
		vector()
		{}
		vector(const vector<T>& v)
		{
			reserve(v.capacity());
			/*for (size_t i = 0; i < v.size(); i++)
			{
				push_back(v[i]);
			}*/
			for (auto& e : v)
			{
				push_back(e);
			}
		}
		template<class inputiterator>
		vector(inputiterator first, inputiterator last)
		{
			while (first != last)
			{
				push_back(*first);
				first++;
			}
		}
		vector(size_t n, const T& val = T())
		{
			reserve(n);
			for (size_t i = 0; i < n;i++)
			{
				push_back(val);
			}
		}
		vector(int n, const T& val = T())
		{
			reserve(n);
			for (int i = 0; i < n; i++)
			{
				push_back(val);
			}
		}
		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_end_of_storage, v._end_of_storage);
		}
		vector<T>& operator=(vector<T> tmp)
		{
			swap(tmp);
			return *this;
		}
		iterator begin()
		{
			return _start;
		}
		iterator end()
		{
			return _finish;
		}
		const_iterator begin()const
		{
			return _start;
		}
		const_iterator end()const
		{
			return _finish;
		}
		void reserve(size_t n)
		{
			if (n > capacity())
			{
				size_t sz = size();
				//size_t cp = capacity() == 0 ? 4 : capacity() * 2;
				T* tmp = new T[n];
				if (_start)
				{
					//memcpy(tmp, _start, sizeof(T) * sz);
					for (size_t i = 0; i < sz; i++)
					{
						tmp[i] = _start[i];
					}
					delete[]_start;
				}
				_start = tmp;
				_finish = _start + sz;
				_end_of_storage = _start + n;
			}
		}
		void push_back(const T& x)
		{
			if (_finish == _end_of_storage)
			{
				reserve(capacity() == 0 ? 4 : capacity() * 2);
			}
			*_finish = x;
			_finish++;
		}
		
		void resize(size_t n, const T& x = T())
		{
			if (n <= size())
			{
				_finish = _start + n;
			}
			else
			{
				reserve(n);
				while (_finish<_start+n)
				{
					*_finish = x;
					_finish++;
				}
			}
		}
		void insert(iterator pos, const T& x)
		{
			assert(pos >= _start);
			assert(pos <= _finish);
			if (_finish == _end_of_storage)
			{
				size_t len = pos-_start;
				reserve(capacity() == 0 ? 4 : capacity() * 2);
				pos = _start + len;
			}
			size_t end = size() - 1;
			while (_start + end >= pos)
			{
				*(_start + end+1) = *(_start + end);
				end--;
			}
			*pos = x;
			_finish++;

		}
		iterator erase(iterator pos)
		{
			assert(pos >= _start);
			assert(pos < _finish);
			iterator end = pos + 1;
			while (end < _finish)
			{
				*(end - 1) = *end;
				end++;
			}
			_finish--;
			return pos;
		}
		T& operator[](size_t pos)
		{
			assert(pos < size());
			return _start[pos];
		}
		const T& operator[](size_t pos)const
		{
			assert(pos < size());
			return _start[pos];
		}
		size_t size()const
		{
			return _finish - _start;
		}
		size_t capacity()const
		{
			return _end_of_storage - _start;
		}

		~vector()
		{
			delete[]_start;
			_start = _finish = _end_of_storage = nullptr;
		}
	private:
		iterator _start=nullptr;
		iterator _finish=nullptr;
		iterator _end_of_storage=nullptr;

	};
	//void Test_vector1()
	//{
	//	vector<int> v;
	//	v.push_back(1);
	//	v.push_back(2);
	//	v.push_back(3);
	//	v.push_back(4);
	//	//v.push_back(5);

	//	for (size_t i = 0; i < v.size(); i++)
	//	{
	//		cout << v[i] << " ";
	//	}
	//	cout << endl;

	//	vector<int>::iterator it = v.begin();
	//	while (it != v.end())
	//	{
	//		cout << *it << " ";
	//		it++;
	//	}
	//	cout << endl;

	//	for (auto e : v)
	//	{
	//		cout << e << " ";
	//	}
	//	cout << endl;
	//}
	//void Test_vector2()
	//{
	//	vector<int> v;
	//	v.resize(10);
	//	for (size_t i = 0; i < v.size(); i++)
	//	{
	//		cout << v[i] << " ";
	//	}
	//	cout << endl;
	//	
	//	vector<string> v1;
	//	v1.resize(10);
	//	for (auto e : v1)
	//	{
	//		cout << e << " ";
	//	}
	//	cout << endl;
	//}
	//void Test_vector3()
	//{
	//	vector<int> v;
	//	v.push_back(1);
	//	v.push_back(2);
	//	v.push_back(3);
	//	v.push_back(4);
	//	//v.push_back(5);

	//	v.insert(v.begin() + 2, 10);
	//	for (auto e : v)
	//	{
	//		cout << e << " ";
	//	}
	//	cout << endl;

	//	v.erase(v.begin() + 2);
	//	for (auto e : v)
	//	{
	//		cout << e << " ";
	//	}
	//	cout << endl;
	//}
	//void Test_vector4()
	//{
	//	vector<int> v;
	//	v.push_back(1);
	//	v.push_back(2);
	//	v.push_back(2);
	//	v.push_back(4);
	//	v.push_back(5);
	//	v.push_back(6);
	//	vector<int>::iterator it = v.begin();
	//	while (it != v.end())
	//	{
	//		if (*it % 2 == 0)
	//		{
	//			it=v.erase(it);
	//		}
	//		else
	//		{
	//			it++;
	//		}
	//	}
	//	for (auto e : v)
	//	{
	//		cout << e << " ";
	//	}
	//	cout << endl;
	//}
	//void Test_vector5()
	//{
	//	vector<string> v;
	//	v.push_back("1111111111111");
	//	v.push_back("1111111111111");
	//	v.push_back("1111111111111");
	//	v.push_back("1111111111111");
	//	v.push_back("1111111111111");
	//	for (auto e : v)
	//	{
	//		cout << e << " ";
	//	}
	//	cout << endl;
	//}
	//void Test_vector6()
	//{
	//	vector<int> v1;
	//	v1.push_back(1);
	//	v1.push_back(2);
	//	v1.push_back(3);
	//	v1.push_back(4);
	//	v1.push_back(5);
	//	for (auto e : v1)
	//	{
	//		cout << e << " ";
	//	}
	//	cout << endl;
	//	vector<int> v2;
	//	v2.push_back(10);
	//	v2.push_back(20);
	//	v2.push_back(30);
	//	v2.push_back(40);
	//	v2.push_back(50);
	//	v1 = v2;
	//	for (auto e : v1)
	//	{
	//		cout << e << " ";
	//	}
	//	cout << endl;
	//}
	//void Test_vector7()
	//{
	//	//vector<string> v(10,"ssss");
	//	vector<int> v(10, 0);
	//	for (auto e : v)
	//	{
	//		cout << e << " ";
	//	}
	//	cout << endl;
	//}
}

